package com.huatai.exchange.bootup.controller;

import com.faneui.common.bean.FaneuiBaseResp;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.DateFormatUtils;
import com.huatai.exchange.facade.DataFileExchangeController;
import com.huatai.exchange.service.ExcChannelInfoService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping(value = "exchange")
@Api("数据文件交换执行")
public class DataFileExchangeControllerImpl extends AbstractRestController implements DataFileExchangeController {

    private static final Logger logger = LoggerFactory.getLogger(DataFileExchangeController.class);

    @Autowired
    private ExcChannelInfoService excChannelInfoService;

    @RequestMapping(value = "/execute/{exchangeInfoId}", method = RequestMethod.GET)
    @ResponseBody
    public FaneuiBaseResp exchangeDataFile(@PathVariable String exchangeInfoId ,@RequestParam(required = false) String occurDate) throws BusinessException {
        FaneuiBaseResp resp = new FaneuiBaseResp();
        try{
            if(StringUtils.isBlank(occurDate)) {
                occurDate = DateFormatUtils.yyyyMMdd.format(new Date()) ;
            }
            excChannelInfoService.executeExchangeDataFile(exchangeInfoId,occurDate);
        }catch (Exception ex) {
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

}
